#include "DS1307.h"

#define DS1307_ADDRESS	0xD0

DS1307_Data DS_Data;//RTC数据

/*外设初始化*/
void DS1307_Init(void)
{
	MyI2C_Init();
}

/*发送一个字节*/
void DS1307_W_Reg(uint8_t reg_address , uint8_t data)
{
	MyI2C_Start();
	MyI2C_SendByte(DS1307_ADDRESS);
	MyI2C_ReceiveAck();
	MyI2C_SendByte(reg_address);
	MyI2C_ReceiveAck();
	MyI2C_SendByte(data);
	MyI2C_ReceiveAck();
	MyI2C_Stop();
}

/*接收一个字节*/
uint8_t DS1307_R_Reg(uint8_t reg_address)
{
	uint8_t data;
	
	/*写方式呼叫从机，将从机指针指向指定的reg*/
	MyI2C_Start();
	MyI2C_SendByte(DS1307_ADDRESS);
	MyI2C_ReceiveAck();
	MyI2C_SendByte(reg_address);
	MyI2C_ReceiveAck();
	
	/*读方式开始读reg地址后面的内容*/
	MyI2C_Start();
	MyI2C_SendByte(DS1307_ADDRESS | 0x01);
	MyI2C_ReceiveAck();
	data = MyI2C_ReceiveByte();
	MyI2C_SendAck(1);
	MyI2C_Stop();
	
	return data;
}

/*读取时间*/
void Read_DS1307_Data(void)
{
	DS_Data.hour = DS1307_R_Reg(0x02);	//时
	DS_Data.min = DS1307_R_Reg(0x01);	//分
	DS_Data.sec = DS1307_R_Reg(0x00);	//秒
}

void DS_Show_Time(void)
{
	Read_DS1307_Data();
	OLED_Printf(0,0,OLED_8X16,"Time %.2x:%.2x:%.2x",DS_Data.hour,DS_Data.min,DS_Data.sec);
	OLED_UpdateArea(0,0,128,16);
}
